package com.soriya.nestlive.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soriya.nestlive.entity.PageResult;
import com.soriya.nestlive.entity.UserEntity;
import com.soriya.nestlive.entity.param.LoginParam;
import com.soriya.nestlive.entity.param.UserListParam;
import com.soriya.nestlive.entity.vo.InfoVo;
import com.soriya.nestlive.service.UserService;
import com.soriya.nestlive.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{current}/{size}")
    public Result<?> list(@PathVariable Integer current, @PathVariable Integer size, UserListParam param) {
        Page<UserEntity> page = new Page<>(current, size);
        PageResult<UserEntity> result = userService.pageQuery(page, param);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result<UserEntity> getById(@PathVariable Long id) {
        UserEntity data = userService.getById(id);
        return Result.success(data);
    }

    @PostMapping
    public Result<?> save(@RequestBody UserEntity entity) {
        boolean save = userService.save(entity);
        return Result.update(save);
    }

    @PutMapping
    public Result<?> updateById(@RequestBody UserEntity entity) {
        boolean b = userService.updateById(entity);
        return Result.update(b);
    }

    @DeleteMapping("/{id}")
    public Result<?> removeById(@PathVariable Long id) {
        boolean b = userService.removeById(id);
        return Result.update(b);
    }

}
